<template>
  <div class="register">
    <el-form ref="formName" :model="form" :rules="rules" label-width="180px" class="register-service">
      <h3 class="title">大公鸡智慧消防系统</h3>
      <el-row>
        <el-col :span="12">
          <el-form-item label="公司名" prop="nickName">
            <el-input v-model="form.nickName" placeholder="请输入公司名" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="法人姓名" prop="legalPersonName">
            <el-input v-model="form.legalPersonName" placeholder="请输入法人姓名" autocomplete="new-password" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="技术负责人姓名" prop="technicalDirectorName">
            <el-input v-model="form.technicalDirectorName" placeholder="请输入技术负责人姓名" autocomplete="new-password"
              aria-autocomplete="none" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户性别" prop="sex">
            <el-select v-model="form.sex" placeholder="请选择性别" style="width: 100%;">
              <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.userId == undefined" label="用户密码" prop="password">
            <el-input v-model="form.password" placeholder="请输入用户密码" type="password" maxlength="20" show-password />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="技术负责人手机号" prop="technicalDirectorPhone">
            <el-input v-model="form.technicalDirectorPhone" placeholder="请输入技术负责人手机号" maxlength="30" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="企业统一社会信用代码" prop="creditCode">
            <el-input v-model="form.creditCode" placeholder="请输入企业统一社会信用代码" maxlength="30" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工商营业执照登记地址" prop="businessLicenseAddress">
            <el-input v-model="form.businessLicenseAddress" placeholder="请输入工商营业执照登记地址" maxlength="30" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="手机号码" prop="phonenumber">
            <el-input v-model="form.phonenumber" placeholder="请输入手机号码" maxlength="11" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="身份证号码" prop="idCard">
            <el-input v-model="form.idCard" placeholder="请输入身份证号码" maxlength="18" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="邮政编码" prop="postcode">
            <el-input v-model="form.postcode" placeholder="请输入邮政编码" maxlength="6" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开户银行" prop="bankName">
            <el-input v-model="form.bankName" placeholder="请输入开户银行" maxlength="200" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="银行账号" prop="bankNumber">
            <el-input v-model="form.bankNumber" placeholder="请输入银行账号" maxlength="20" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email" placeholder="请输入邮箱" maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地区选择" prop="singleSelectTreeVal">
            <el-select ref="singleTree" v-model="singleSelectTreeVal" popper-class="select-tree" value-key="id" style="width: 100%;">
              <el-option :value="singleSelectTreeKey" class="options">
                <el-tree ref="singleSelectTree" :current-node-key="singleSelectTreeKey" :expand-on-click-node="false"
                  :data="dataArea" :props="defaultProps" accordion @node-click="selectTreeNodeClick">
                  <span slot-scope="{ node }" class="vab-custom-tree-node">
                    <span class="vab-tree-item">{{ node.data.name }}</span>
                  </span>
                </el-tree>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" maxlength="256"></el-input>
          </el-form-item>
        </el-col> -->

        <!-- 上传图片 -->
        <el-col :span="13">
          <el-form-item label="技术负责人身份证" prop="technicalDirectorIdCard" class="is-required">
            <ImageUpload v-model="form.technicalDirectorIdCard" :limit="2" :isShowTip="false" :isState="false" />
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="工商营业执照副本" prop="businessLicenseCopy" class="is-required">
            <ImageUpload v-model="form.businessLicenseCopy" :limit="1" :isShowTip="false" :isState="false" />
          </el-form-item>
        </el-col>
        <el-col :span="13">
          <el-form-item label="资质证书" prop="qualification" class="is-required">
            <ImageUpload v-model="form.qualification" :limit="1" :isShowTip="false" :isState="false" />
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="技术负责人注册证书" prop="technicalDirectorCertificate" class="is-required">
            <ImageUpload v-model="form.technicalDirectorCertificate" :limit="1" :isShowTip="false" :isState="false" />
          </el-form-item>
        </el-col>
      </el-row>
      <div>
        <div style="float: left;">
          <el-button type="primary" @click="handleRegister" style="width: 240%;">注册</el-button>
        </div>
        <div style="float: right;">
          <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
        </div>
      </div>
    </el-form>

    <!--  底部  -->
    <!-- <div class="el-register-footer">
      <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span>
    </div> -->
  </div>
</template>

<script>
import {
  getCodeImg,
  orgRegister
} from "@/api/login";
import {
  regionTree
} from "@/api/system/user";
export default {
  name: "Register",
  dicts: ["sys_normal_disable", "sys_user_sex"],
  data() {
    // 文件上传校验规则
    const fileMustUpload1 = (rule, value, callback) => {
      if (
        this.form.technicalDirectorIdCard == undefined ||
        this.form.technicalDirectorIdCard == null ||
        this.form.technicalDirectorIdCard == ""
      ) {
        callback("请上传技术负责人身份证");
      } else {
        callback();
      }
    };
    const equalToPassword = (rule, value, callback) => {
      if (this.form.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    const fileMustUpload2 = (rule, value, callback) => {
      if (
        this.form.businessLicenseCopy == undefined ||
        this.form.businessLicenseCopy == null ||
        this.form.businessLicenseCopy == ""
      ) {
        callback("请上传工商营业执照副本");
      } else {
        callback();
      }
    };
    const fileMustUpload3 = (rule, value, callback) => {
      if (
        this.form.qualification == undefined ||
        this.form.qualification == null ||
        this.form.qualification == ""
      ) {
        callback("请上传资质证书");
      } else {
        callback();
      }
    };
    const fileMustUpload4 = (rule, value, callback) => {
      if (
        this.form.idCardPhoto == undefined ||
        this.form.idCardPhoto == null ||
        this.form.idCardPhoto == ""
      ) {
        callback("请上传身份证照片");
      } else {
        callback();
      }
    };
    const fileMustUpload5 = (rule, value, callback) => {
      if (
        this.form.technicalDirectorCertificate == undefined ||
        this.form.technicalDirectorCertificate == null ||
        this.form.technicalDirectorCertificate == ""
      ) {
        callback("请上传技术负责人注册证书");
      } else {
        callback();
      }
    };
    const fileMustUpload6 = (rule, value, callback) => {
      if (
        this.form.faceUrl == undefined ||
        this.form.faceUrl == null ||
        this.form.faceUrl == ""
      ) {
        callback("请上传人脸采集");
      } else {
        callback();
      }
    };
    const checkStartTime = (rule, value, callback) => {
      if (!this.form.qualificationEndDate) {
        callback()
      } else {
        if (this.form.qualificationEndDate <= value) {
          callback(new Error('证书开始时间不能晚于证书结束时间'));
        } else {
          callback()
        }
      }
    };
    const checkEndTime = (rule, value, callback) => {
      if (!this.form.qualificationStartDate || !value) {
        callback()
      } else {
        if (this.form.qualificationStartDate >= value) {
          callback(new Error('证书结束时间不能早于证书开始时间'));
        } else {
          callback()
        }
      }
    };
    return {
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      defaultProps: {
        children: "children",
        label: "label",
      },
      singleSelectTreeVal: "", //单选树默认label值
      singleSelectTreeKey: "", //单选树默认key值
      dataArea: [],
      roleOptions: [],
      // 表单参数
      form: {
        technicalDirectorCertificate: "",
        legalPersonName: ''
      },
      codeUrl: "",
      // 表单校验
      rules: {
        nickName: [{
          required: true,
          message: "公司名不能为空",
          trigger: "blur"
        }, {
          min: 2,
          max: 20,
          message: "公司名长度必须介于 2 和 30 之间",
          trigger: "blur"
        },],
        bankName: [{
          required: true,
          message: "开户行不能为空",
          trigger: "blur"
        },],
        legalPersonName: [{
          required: true,
          message: "法人姓名不能为空",
          trigger: "blur"
        },],
        technicalDirectorName: [{
          required: true,
          message: "姓名不能为空",
          trigger: "blur"
        },],
        businessLicenseAddress: [{
          required: true,
          message: "地址不能为空",
          trigger: "blur"
        },],
        password: [{
          required: true,
          message: "用户密码不能为空",
          trigger: "blur"
        },
        {
          min: 5,
          max: 20,
          message: "用户密码长度必须介于 5 和 20 之间",
          trigger: "blur",
        },
        ],
        email: [{
          type: "email",
          message: "请输入正确的邮箱地址",
          trigger: "blur",
        },],
        roleIds: [{
          required: true,
          message: "至少选择一个角色",
          trigger: "change",
        },],
        phonenumber: [{
          required: true,
          pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
          message: "请输入正确的手机号码",
          trigger: "blur",
        },],
        technicalDirectorPhone: [{
          required: true,
          pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
          message: "请输入正确的手机号码",
          trigger: "blur",
        },],
        creditCode: [{
          required: true,
          pattern: /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/,
          message: "请输入正确的企业信用代码",
          trigger: "blur",
        },],
        idCard: [{
          required: true,
          pattern: /^\d{17}[\dX]$/,
          message: "请输入正确的身份证号码",
          trigger: "blur",
        },],
        bankNumber: [{
          required: true,
          pattern: /\b\d{9,}\b/,
          message: "请输入正确的银行账号",
          trigger: "blur",
        },],
        postcode: [{
          required: true,
          pattern: /^[1-9]\d{5}$/,
          message: "请输入正确的邮政编码",
          trigger: "blur",
        },],
        technicalDirectorIdCard: [{
          validator: fileMustUpload1,
          trigger: "change"
        },],
        businessLicenseCopy: [{
          validator: fileMustUpload2,
          trigger: "change"
        },],
        qualification: [{
          validator: fileMustUpload3,
          trigger: "change"
        }],
        idCardPhoto: [{
          validator: fileMustUpload4,
          trigger: "change"
        }],
        technicalDirectorCertificate: [{
          validator: fileMustUpload5,
          trigger: "change"
        },],
        faceUrl: [{
          validator: fileMustUpload6,
          trigger: "change"
        },],
        qualificationStartDate: [{
          validator: checkStartTime,
          trigger: 'change'
        }],
        qualificationEndDate: [{
          validator: checkEndTime,
          trigger: 'change'
        }]
      },
      loading: false,
      captchaEnabled: true
    };
  },
  created() {
    this.getCode();
    regionTree().then(res => {
      this.dataArea = res.data
    })

  },
  methods: {
    selectTreeNodeClick(data) {
      this.singleSelectTreeVal = data.name;
      this.singleSelectTreeKey = data.id;
      this.$refs.singleTree.blur();
    },
    // 取消按钮
    cancel() {
      this.reset();
      this.userTypeState = false
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.form.uuid = res.uuid;
        }
      });
    },
    handleRegister() {
      this.$refs["formName"].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.form.userName = this.form.nickName
          this.form.regionId = this.singleSelectTreeKey
          orgRegister(this.form).then(res => {
            const username = this.form.userName;
            this.$alert("<font color='red'>恭喜你，您的账号注册成功！等待管理员审核！</font>", '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              this.$router.push("/login");
            }).catch(() => { });
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          })

        }
      });
    }
  }
};

</script>

<style rel="stylesheet/scss" lang="scss">
.select-tree {
  .el-select-dropdown__item {
    padding: 5px 10px;
    height: auto;
    overflow: hidden;
  }

  .el-select-dropdown__item.hover,
  .el-select-dropdown__item:hover {
    background-color: #fff;
  }

  .el-select-dropdown__item.selected {
    font-weight: normal;
  }
}

.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 140%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: 140% auto;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.register-service {
  border-radius: 6px;
  background: #ffffff;
  width: 1000px;
  margin: 10px;
  padding: 25px;
  box-sizing: border-box;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.register-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.register-code-img {
  height: 38px;
}
</style>
